package com.stageadminserver.adminserver.service.impl;

import cn.dev33.satoken.secure.SaSecureUtil;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stageadminserver.adminserver.mapper.ManagerMapper;
import com.stageadminserver.adminserver.service.ManagerService;
import com.stagecommon.common.pojo.Manager;
import com.stagecommon.common.pojo.RespBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author: yxl
 * @since: 2021-12-05
 **/
@Slf4j
@Service
public class ManagerServiceImpl extends ServiceImpl<ManagerMapper, Manager> implements ManagerService {

    @Autowired
    private ManagerMapper managerMapper;

    @Override
    public RespBean managerLogin(String name, String password) {
        if (!Objects.equals(null, managerMapper.findManager(name, SaSecureUtil.md5(password)))){
            StpUtil.login(name);
            return RespBean.loginSuccessReturnToken("登录成功", 200, managerMapper.selectOne(new QueryWrapper<Manager>().select("manager_id","manager_name").eq("manager_name",name)), StpUtil.getTokenValue());
        } else {
            return RespBean.operationFail("登录失败");
        }

    }

    @Override
    public RespBean<Manager> getManagerById(Integer managerId) {
        return RespBean.operationSuccess("查询成功！", 200, managerMapper.selectById(managerId));
    }

}
